from nmj.core.constants import REGEXP_SHOW_WITH_EPISODE, REGEXP_SHOW_WITH_SEASON, \
	REGEXP_SHOW_WITH_SEASON_EPISODE, REGEXP_MOVIES
from nmj.scanners.regexp_based_scanner import RegexpBasedScanner, TVShowCleaner, \
	MovieCleaner
import logging
import unittest

_LOGGER = logging.getLogger(__name__)

class FakeScanner:
	def __init__(self):
		self.searches = []

	def search(self, show, season=None, episode=None):
		self.searches.append((show, season, episode))

class RegExp_TestCase(unittest.TestCase):

	def test_scanner_regexp(self):
		"""Test use the right scanner depending on the regexp (episode only, season/episode, movies) """
		self.__test_cleaning("Naruto_-_228_", "naruto", episode="228")
		self.__test_cleaning("Lost S01E01", "lost", season="01", episode="01")
		self.__test_cleaning("FlashForward S01E01", "flashforward", season="01", episode="01")
		self.__test_cleaning("The A Team", "the a team")
		self.__test_cleaning("minuscule s04", "minuscule", season="04")
		self.__test_cleaning("minuscule s01e03", "minuscule", season="01", episode="03")
		self.__test_cleaning("Horrible.Bosses.MULTi.1080p.BluRay.x264-4kHD", "horrible bosses")
		self.__test_cleaning("Ice.Age.A.Mammoth.Christmas.FRENCH.720p.WEB-DL.DD2.0.H.264-NeXu14", "ice age a mammoth christmas")
		self.__test_cleaning("LA FEE CLOCHETTE L'EXPEDITION FEERIQUE.LiMiTED.French.BRRip.XviD.AC3-TFTD", "la fee clochette l'expedition feerique")
		self.__test_cleaning("Les.Aristochats.1970.TRUEFRENCH.1080p.HDTV.x264-BigF", "les aristochats")
		self.__test_cleaning("The.Mentalist.S02E21.FRENCH.LD.HDTV.XviD-JMT", "the mentalist", season="02", episode="21")

	def test_cleaning(self):
		"""Test cleaning is doing its job (not too much)"""
		self.__test_cleaning("2012", "2012")
		self.__test_cleaning("[SSF] Fairy Tail_-_123_", "fairy tail", episode="123")
		self.__test_cleaning("LOL truefrench", "lol")
		self.__test_cleaning("Avatar.2011", "avatar")
		self.__test_cleaning("Avatar (2011)", "avatar")
		self.__test_cleaning("[RipperTeam]_Avatar_2011_DVDRip_TRUEFRENCH", "avatar")
		self.__test_cleaning("Thats.My.Boy.2012.FRENCH.BRRiP.XviD.AC3-AUTOPSiE", "thats my boy")
		self.__test_cleaning("Quantum.Of.Solace.TRUEFRENCH.SUBFORCED.DVDRip.XviD.AC3-PoneyClub", "quantum of solace")



	def __test_cleaning(self, title, expected_clean_title, season=None, episode=None):
		fake_scanner = FakeScanner()
		scanner = RegexpBasedScanner(
			(REGEXP_SHOW_WITH_EPISODE, fake_scanner, TVShowCleaner(), "TV Show with episode"),
			(REGEXP_SHOW_WITH_SEASON_EPISODE, fake_scanner, TVShowCleaner(), "TV Show with season and episode"),
			(REGEXP_SHOW_WITH_SEASON, fake_scanner, TVShowCleaner(), "TV Show with season"),
			(REGEXP_MOVIES, fake_scanner, MovieCleaner(), "Movie"),
		)
		scanner.search(title)
		#print ord(fake_scanner.searches[0][0][-1]), ord(' '), fake_scanner.searches[0][0].strip()
		self.failUnlessEqual([(expected_clean_title, season, episode)], fake_scanner.searches)

if __name__ == "__main__":
	logging.basicConfig(format='%(asctime)-15s %(message)s', level=logging.DEBUG)
	unittest.main()


